<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { userStore } from '@/stores/loginStore'
import router from '@/router'
const userInfo = userStore()
const form = reactive({
  username: '',
  password: ''
})

// 校验规则
const loginRules = ref({
  username: [
    { required: true, message: '账号不能为空！', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在3-20位之间！', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空！', trigger: 'blur' },
    { min: 3, max: 60, message: '密码长度在3-60位之间！', trigger: 'blur' },
  ]
})
// 关联表单
const loginFormRef = ref()
//登录
const submit = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid: any, fields: any) => {
    if (valid) {
      await userInfo.userLogin(form)
      // console.log(res);
      if (userInfo.isLogin) {
        router.push('/index')
      }
    } else {
      console.log('error submit!', fields)
    }
  })

}
const reset = () => {
  form.username = ''
  form.password = ''
}

</script>
<template>
  <div class="login-container">
    <div class="login-card">
      <h2>用户登录</h2>
      <el-form ref="loginFormRef" :rules="loginRules" :model="form" label-width="80px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-button type="primary" @click="submit" class="login-btn">登录</el-button>
            </el-col>
            <el-col :span="12">
              <el-button @click="reset" class="reset-btn">重置</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #F0F9F5;
}

.login-card {
  background-color: #ffffff;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 400px;
}

.login-card h2 {
  text-align: center;
  color: #333333;
  margin-bottom: 20px;
}

.el-input__inner {
  border-radius: 6px;
  border: 1px solid #C0E0D0;
  transition: border-color 0.3s ease;
}

.el-input__inner:focus {
  border-color: #A0E1D0;
}

.login-btn {
  background-color: #D0F4DE;
  border-color: #A0E1D0;
  color: #333333;
  width: 100%;
  border-radius: 6px;
  transition: background-color 0.3s ease;
}

.login-btn:hover {
  background-color: #A0E1D0;
}

.reset-btn {
  width: 100%;
  border-radius: 6px;
}
</style>